//
//  OnlineSubHeadView.m
//  GC_OC
//
//  Created by lbh on 2022/4/6.
//

#import "OnlineSubHeadView.h"

@implementation OnlineSubHeadView

- (instancetype)initWithFrame:(CGRect)frame{
    self = [super initWithFrame:frame];
    if (self) {
        [self makeUI];
    }
    return self;
}
#pragma mark --Priavte
- (void)makeUI{

    [self addSubview:self.bgView];
    [self.bgView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.edges.mas_equalTo(0);
    }];
    
    [self.bgView addSubview:self.titleLabel];
    [self.titleLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(wid(20));
        make.left.mas_equalTo(wid(20));
    }];
    
    [self.bgView addSubview:self.collectionView];
    [self.collectionView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(self.titleLabel.mas_bottom).offset(wid(0));
        make.left.mas_equalTo(wid(0));
        make.right.mas_equalTo(-wid(0));
        make.bottom.mas_equalTo(0);
    }];
}
- (void)setDataSource:(NSArray *)dataSource{
    _dataSource = dataSource;
    self.titleLabel.hidden = NO;
    if (self.dataSource.count == 0) {
        self.titleLabel.hidden = YES;
    }
    [self.collectionView reloadData];
}
#pragma mark --UICollectionViewDelegate
- (NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView{
    return 1;
}
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section{
    
    return self.dataSource.count;
}
- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
    
    OnlineSubHeadViewCollCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"OnlineSubHeadViewCollCell" forIndexPath:indexPath];
    NSDictionary *dict = self.dataSource[indexPath.row];
    NSString *media_image = dict[@"teacher_image"];
    NSString *teacher_introduce = dict[@"teacher_introduce"];
    NSString *name = dict[@"teacher_name"];
    NSString *teacher_sex = dict[@"teacher_sex"];
    NSString *zLogo = @"logo_nan01";
    if (teacher_sex.intValue == 2) {
        zLogo = @"Logo_nu01";
    }
    media_image = [media_image stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
    [cell.imageView sd_setImageWithURL:[NSURL URLWithString:media_image] placeholderImage:[UIImage imageNamed:zLogo]];
//    name = [NSString stringWithFormat:@"姓名:%@",name];
//    teacher_sex = [NSString stringWithFormat:@"性别:%@",teacher_sex.intValue ==2?@"女":@"男"];
//    teacher_introduce = [NSString stringWithFormat:@"简介:%@",teacher_introduce];
//
//    cell.titleLabel.attributedText = [self createAttributedStr:NSMakeRange(0, 2) withArrtStr:name];
//    cell.sexLabel.attributedText = [self createAttributedStr:NSMakeRange(0, 2) withArrtStr:teacher_sex];
//    cell.descLabel.attributedText = [self createAttributedStr:NSMakeRange(0, 2) withArrtStr:teacher_introduce];
    cell.titleLabel.text = name;
    cell.sexLabel.text = teacher_sex.intValue ==2?@"女":@"男";
    cell.descLabel.text = teacher_introduce;
    return cell;
}
- (NSMutableAttributedString *)createAttributedStr:(NSRange)range withArrtStr:(NSString *)string{
    
    NSMutableAttributedString *AttributedStr = [[NSMutableAttributedString alloc]initWithString:string];
    [AttributedStr addAttribute:NSFontAttributeName
                          value:[UIFont fontWithName:@"PingFangSC-Medium" size:14]
                          range:range];
    [AttributedStr addAttribute:NSForegroundColorAttributeName
                          value:UIColor.blackColor
                          range:range];
    
    return AttributedStr;
}
- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath {
    
//    return CGSizeMake(SCREEN_WIDTH *0.8, 160);

    return CGSizeMake(140, 180);
}
- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath {
    OnLineTeacherDetailVC *vc = [[OnLineTeacherDetailVC alloc]init];
    NSDictionary *dict = self.dataSource[indexPath.row];
    vc.dataDict = dict;
    vc.medilassID = self.mediClassID;
    vc.hidesBottomBarWhenPushed=YES;
    [[KFTools getCurrentVC].navigationController pushViewController:vc animated:NO];
}
#pragma mark --Lazy
- (UIView *)bgView{
    if (!_bgView) {
        _bgView = [[UIView alloc]init];
        _bgView.backgroundColor = [UIColor clearColor];
    }
    return _bgView;
}
- (UILabel *)titleLabel{
    if (!_titleLabel) {
        _titleLabel = [[UILabel alloc]init];
        _titleLabel.font = [UIFont fontWithName:@"Helvetica-Bold" size:FontSize(14)];;
        _titleLabel.textColor = UIColor.blackColor;
        _titleLabel.text = @"教师风采";
    }
    return _titleLabel;
}
- (UICollectionView *)collectionView {
    
    if (!_collectionView) {
        
        UICollectionViewFlowLayout *layout = [[UICollectionViewFlowLayout alloc] init];
        layout.minimumLineSpacing = wid(0);
        layout.minimumInteritemSpacing = wid(0);
        layout.scrollDirection = UICollectionViewScrollDirectionHorizontal;
        layout.sectionInset = UIEdgeInsetsMake(0, 0, 0, 0);
        _collectionView = [[UICollectionView alloc] initWithFrame:[UIScreen mainScreen].bounds collectionViewLayout:layout];
        _collectionView.backgroundColor = AppMainGrayColor;
        _collectionView.delegate = self;
        _collectionView.dataSource = self;
        _collectionView.scrollEnabled = YES;
        [_collectionView registerClass:[OnlineSubHeadViewCollCell class] forCellWithReuseIdentifier:@"OnlineSubHeadViewCollCell"];
        _collectionView.userInteractionEnabled = YES;
        _collectionView.showsHorizontalScrollIndicator = NO;
        _collectionView.layer.masksToBounds = YES;
        _collectionView.layer.cornerRadius = 0;
        if (@available(iOS 11.0, *)) {
            
            _collectionView.contentInsetAdjustmentBehavior = UIScrollViewContentInsetAdjustmentNever;
        }
    }
    return _collectionView;
}
@end

@implementation OnlineSubHeadViewCollCell
- (instancetype)initWithFrame:(CGRect)frame{
    self = [super initWithFrame:frame];
    if (self) {
        [self makeUI];
    }
    return self;
}
#pragma mark --Private
- (void)makeUI{
    [self.contentView addSubview:self.bgView];
    [self.bgView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(5);
        make.left.mas_equalTo(5);
        make.right.mas_equalTo(-5);
        make.bottom.mas_equalTo(-0);
    }];
    
    [self.bgView addSubview:self.imageView];
    [self.imageView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(20);
        make.left.mas_equalTo(25);
//        make.width.mas_equalTo(120);
//        make.height.mas_equalTo(160);
        make.width.mas_equalTo(90);
        make.height.mas_equalTo(123);
//        make.bottom.mas_equalTo(0);
    }];
    
    [self.bgView addSubview:self.titleLabel];
    [self.titleLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(self.imageView.mas_bottom).offset(5);
//        make.left.mas_equalTo(self.imageView.mas_left).offset(5);
        make.centerX.mas_equalTo(self.imageView.mas_centerX);
    }];
    
//    [self.bgView addSubview:self.sexLabel];
//    [self.sexLabel mas_makeConstraints:^(MASConstraintMaker *make) {
//        make.top.mas_equalTo(self.titleLabel.mas_bottom).offset(5);
//        make.left.mas_equalTo(self.titleLabel.mas_left);
//    }];
//
//    [self.bgView addSubview:self.descLabel];
//    [self.descLabel mas_makeConstraints:^(MASConstraintMaker *make) {
//        make.top.mas_equalTo(self.sexLabel.mas_bottom).offset(5);
//        make.left.mas_equalTo(self.sexLabel.mas_left);
//        make.right.mas_equalTo(-5);
//    }];
}
#pragma mark --lazy
- (UIView *)bgView{
    if (!_bgView) {
        _bgView = [[UIView alloc]init];
        _bgView.backgroundColor = [UIColor clearColor];
//        _bgView.backgroundColor = AppMainGrayColor;
        [self setUpViewLayer:_bgView];
    }
    return _bgView;
}
- (UIImageView *)imageView{
    if (!_imageView) {
        _imageView = [[UIImageView alloc]init];
//        _imageView.layer.masksToBounds = YES;
//        _imageView.layer.cornerRadius = 5;
    }
    return _imageView;
}
- (void)setUpViewLayer:(UIView *)vie
{
    vie.layer.shadowColor = [[UIColor blackColor] colorWithAlphaComponent:0.8].CGColor;
    vie.layer.shadowOffset = CGSizeMake(0,0);
    vie.layer.shadowOpacity = 0.1;
    vie.layer.shadowRadius = 5;
    vie.layer.cornerRadius = 5;
}
- (UILabel *)titleLabel{
    if (!_titleLabel) {
        _titleLabel = [[UILabel alloc]init];
        _titleLabel.font = [UIFont boldSystemFontOfSize:16];
        _titleLabel.textColor = [UIColor colorWithRed:60/255.0 green:65/255.0 blue:69/255.0 alpha:1.0];
        _titleLabel.numberOfLines = UIViewContentModeCenter;
        _titleLabel.text = @"标题";
    }
    return _titleLabel;
}
- (UILabel *)sexLabel{
    if (!_sexLabel) {
        _sexLabel = [[UILabel alloc]init];
        _sexLabel.font = [UIFont boldSystemFontOfSize:16];
        _sexLabel.textColor = [UIColor colorWithRed:60/255.0 green:65/255.0 blue:69/255.0 alpha:1.0];
        _sexLabel.numberOfLines = UIViewContentModeCenter;
        _sexLabel.text = @"女";
    }
    return _sexLabel;
}
- (UILabel *)descLabel{
    if (!_descLabel) {
        _descLabel = [[UILabel alloc]init];
        _descLabel.font = [UIFont boldSystemFontOfSize:16];
        _descLabel.textColor = [UIColor colorWithRed:60/255.0 green:65/255.0 blue:69/255.0 alpha:1.0];
        _descLabel.numberOfLines = UIViewContentModeCenter;
        _descLabel.text = @"描述";
        _descLabel.numberOfLines = 4;
    }
    return _descLabel;
}
@end
